package com.rabbitmq.consumer;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

/**
 * @Author： yu.Tan
 * @Date: 2020/8/20 11:10
 * @Description:
 */
@Component
public class Receiver {

    /********************************Direct***************************************************/
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "DirectQueue-1027-1", durable = "true"),
            exchange = @Exchange(value = "DirectExchange-1027", type = ExchangeTypes.DIRECT),
            key = "DirectRouting"
    ))
    @RabbitHandler
    public void prcessDirect(@Payload byte[] body) {
        try {
            String s = new String(body, "UTF-8");
            System.out.println("message is :" + s);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }


    /********************************Topic***************************************************/
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "topic.first", durable = "true"),
            exchange = @Exchange(value = "topicExchange", type = ExchangeTypes.TOPIC),
            key = "topic.first"
    ))
    private void processTopicFirst(@Payload byte[] body) {
        try {
            String s = new String(body, "UTF-8");
            System.out.println("message is :" + s);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "topic.second", durable = "true"),
            exchange = @Exchange(value = "topicExchange", type = ExchangeTypes.TOPIC),
            key = "topic.second"
    ))
    private void processTopicSecond(@Payload byte[] body) {
        try {
            String s = new String(body, "UTF-8");
            System.out.println("message is :" + s);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }


    /********************************Fanout***************************************************/


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "fanout.A", durable = "true"),
            exchange = @Exchange(value = "fanoutExchange", type = ExchangeTypes.FANOUT)
    ))
    private void processFanoutA(@Payload byte[] body) {
        try {
            String s = new String(body, "UTF-8");
            System.out.println("fanout.A receiver message is :" + s);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "fanout.B", durable = "true"),
            exchange = @Exchange(value = "fanoutExchange", type = ExchangeTypes.FANOUT)
    ))
    private void processFanoutB(@Payload byte[] body) {
        try {
            String s = new String(body, "UTF-8");
            System.out.println("fanout.B receiver message is :" + s);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "fanout.C", durable = "true"),
            exchange = @Exchange(value = "fanoutExchange", type = ExchangeTypes.FANOUT)
    ))
    private void processFanoutC(@Payload byte[] body) {
        try {
            String s = new String(body, "UTF-8");
            System.out.println("fanout.C receiver message is :" + s);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

}
